package com.togo.algorithm.util;

import com.togo.algorithm.common.ListNode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author taiyn
 * @Description TODO
 * @Date 上午10:39 2022/4/5
 **/
public class ListNodeUtil {
    private ListNodeUtil() {
        throw new UnsupportedOperationException();
    }

    public static void print(ListNode listNode) {
        ListNode node = listNode;
        List<Integer> list = new ArrayList<>();
        while (node != null) {
            list.add(node.val);
            node = node.next;
        }

        System.out.println(list);
    }

    public static ListNode buildByArray(int[] arr) {
        if (arr == null) throw new NullPointerException();
        if (arr.length == 0) return null;

        ListNode head = new ListNode(arr[0]);
        ListNode cur = head;
        if (arr.length == 1) return head;

        for(int i = 1; i < arr.length; i++) {
            ListNode node = new ListNode(arr[i]);
            cur.next = node;
            cur = node;
        }

        return head;
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3};
        ListNode listNode = ListNodeUtil.buildByArray(arr);
        ListNodeUtil.print(listNode);
    }
}
